//
//  HYMyCollectionViewController.m
//  XPlayer
//
//  Created by liguoqing on 2023/10/20.
//

#import "HYMyCollectionViewController.h"
#import "HYMyCollectionViewModel.h"
#import "HYOtherVideoListCell.h"
#import "HYVideoRoomViewController.h"
#import "HYAlertActionView.h"
#import "HYAudioRoomViewController.h"


@interface HYMyCollectionViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) HYMyCollectionViewModel *viewModel;

@end

@implementation HYMyCollectionViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationBarView.navagationBarStyle = Left_right_button_show;
    [self.navigationBarView.rightButton setTitle:@"清空" forState:UIControlStateNormal];
    [self configUI];
    [self bindSignal];
}
//在视频播放器里面取消收藏后返回页面的时候重新获取数据
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
}
- (void)rightButtonClick
{
    @weakify(self);
    [HYAlertActionView showViewControler:self title:NSLocalizedString(@"温馨提示", nil) message:NSLocalizedString(@"是否清空收藏记录！", nil) actionTitle:@[NSLocalizedString(@"确定", nil),NSLocalizedString(@"取消", nil)] sureBlock:^{
        @strongify(self);
        [self.viewModel clearAllCollectionHistory];
    } cancelBlock:nil];
}

- (void)configUI
{
    [self.view addEmptyViewWithPlaceImageName:@"icon_empty_content" placeTitle:NSLocalizedString(@"暂无收藏数据", nil)];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navigationBarView.mas_bottom);
        make.left.bottom.right.equalTo(self.view);
    }];
}

- (void)bindSignal
{
    @weakify(self);
    [self.viewModel.updatedContentSignal subscribeNext:^(id x) {
        @strongify(self);
        self.tableView.hidden = ![x count];
        self.navigationBarView.rightButton.hidden = ![x count];
        [self.tableView reloadData];
    }];
    
    [self.viewModel.playSubject subscribeNext:^(RACTuple *tuple) {
        @strongify(self);
        HYVideoViewModel  *viewModel = tuple.first;
        if ([tuple.second isEqualToString:@"0"]) {
            HYVideoRoomViewController *videoVC = [[HYVideoRoomViewController alloc]initWithViewModel:viewModel];
            [self.navigationController pushViewController:videoVC animated:YES];
        }else{
            HYAudioRoomViewController *videoRoomVC = [[HYAudioRoomViewController alloc]initWithViewModel:viewModel];
            [self.navigationController pushViewController:videoRoomVC animated:YES];
        }
    }];
}

- (void)loadData
{
    [self.viewModel loadData];
}

#pragma mark - UITableViewDelegate && UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger row = [self.viewModel numberOfRowsInSection:section];
    tableView.hidden = !row;
    return row;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HYOtherVideoListCell *videoCell = [tableView dequeueReusableCellWithIdentifier:@"HYOtherVideoListCell" forIndexPath:indexPath];
    [videoCell bindViewModel:[self.viewModel cellForRowAtIndexPath:indexPath]];
    return videoCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.viewModel didSelectRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kItemListHeight+20;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    @weakify(self);
    if(editingStyle == UITableViewCellEditingStyleDelete){
        @strongify(self);
        if([self.viewModel deleteCollectAtIndexPath:indexPath]){
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

#pragma mark - loadLazy
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc]init];
        _tableView.backgroundColor = AppBgThemeColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.contentInset = UIEdgeInsetsMake(2.5, 0, 0, 0);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[HYOtherVideoListCell class] forCellReuseIdentifier:@"HYOtherVideoListCell"];
    }
    return _tableView;
}

- (HYMyCollectionViewModel *)viewModel
{
    if(!_viewModel){
        _viewModel = [[HYMyCollectionViewModel alloc]init];
    }
    return _viewModel;
}

@end

